package org.roulette.resources;

import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.roulette.model.Game;
import org.roulette.model.Player;
import org.roulette.model.Table;

@Path("/game")
public class GameResource {
	
	@GET
	@Produces({ MediaType.APPLICATION_JSON })
	public List<Table> getGame() {
		return Game.getTableList();
	}

	@PUT
	@Path("player/{name}/{password}")
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_JSON })
	public Player addPlayer(@PathParam("name") String name,
			@PathParam("password") String password) {
		Player player = new Player(name, password);
		Game.addPlayer(player);
		return player;
	}

	@GET
	@Path("login/{name}/{password}")
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_JSON })
	public Player loginPlayer(@PathParam("name") String name,
			@PathParam("password") String password) {
		if (Game.checkLogin(name, password)) {
			return Game.getPlayer(name);
		}
		return null;
	}

	@PUT
	@Path("join/{table}")
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_JSON })
	public Table joinTable(@PathParam("table") String id,
			@QueryParam("username") String playerName,
			@QueryParam("password") String password) {
		if (Game.checkLogin(playerName, password)) {
			Game.getTableById(Integer.parseInt(id)).addPlayerToTable(
					Game.getPlayer(playerName));
			return Game.getTableById(Integer.parseInt(id));
		}
		return null;
	}

	@DELETE
	@Path("leavetable")
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_JSON })
	public List<Table> leaveTable(@PathParam("table") String id,
			@QueryParam("username") String playerName,
			@QueryParam("password") String password) {
		if (Game.checkLogin(playerName, password)) {
			Game.removePlayerFromTable(playerName);
			return Game.getTableList();
		}
		return null;
	}

	@GET
	@Path("{table}")
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_JSON })
	public Table getTable(@PathParam("table") String id,
			@QueryParam("username") String playerName,
			@QueryParam("password") String password) {
		if (Game.checkLogin(playerName, password)) {
			return Game.getTableById(Integer.parseInt(id));
		}
		return null;
	}

	@PUT
	@Path("bet/{betfield}/{betvalue}")
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_JSON })
	public Game bet(@PathParam("name") String playerName,
			@PathParam("betfield") String betField,
			@PathParam("betvalue") String betValue,
			@QueryParam("username") String playername,
			@QueryParam("password") String password) {
		if (Game.checkLogin(playername, password)) {
			Game.betOnTable(playername, betField, Integer.parseInt(betValue));
		}
		return Game.getInstance();
	}

	@GET
	@Path("{table}/{player}")
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_JSON })
	public Player getPlayerByTableId(@PathParam("table") String tableId,
			@PathParam("player") String playerName) {
		return Game.getPlayerByTableId(Integer.parseInt(tableId), playerName);
	}

}
